/**
 * 
 */
package userController;


import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import userEntity.LoginEntity;
import userLogic.LoginLogic;
import validator.Validator;
import constants.Constants;

/**
 * @author Yokesh Kumar
 *
 */
public class LoginController implements Initializable{

	@FXML
	private TextField loginId;
	@FXML
	private PasswordField pwd;
	@FXML
	private Label message;
	@FXML
	private Stage PrevStage;
	
    /**
     * This method is executed when user clicks login button
     * Precondition: user starts the application and fill in username and password
     * Postcondition: appropriate form is loaded depending on the type of user
     * @param event
     * @throws IOException
     * @throws ClassNotFoundException
     */
	@FXML
	public void Login(ActionEvent event) throws IOException, ClassNotFoundException{
		try
		{
		LoginLogic loginLogic = new LoginLogic();
		LoginEntity login = new LoginEntity();
		if(loginId  == null || loginId.getText().isEmpty()){ 
			message.setText("Please enter the LoginId");
		}
		else if(pwd  == null || pwd.getText().isEmpty()){
			message.setText("Please enter the password");
		}
		else{
			login.setLoginId(loginId.getText().trim());
			login.setPwd(pwd.getText().trim());
			
			String role = loginLogic.fetchLoginRole(login);

			if(role.equalsIgnoreCase(Constants.CUSTOMER)){
				Stage custStage = new Stage();
				FXMLLoader myLoader = new  FXMLLoader(getClass().getResource(Navigator.CUSTOMERFXML));
				Pane custPane = (Pane)myLoader.load();
				CustomerController custControl = (CustomerController) myLoader.getController();
				Navigator.setCustomer(custControl);
				String phoneNumber;
				phoneNumber = loginLogic.fetchPhoneNumber(login);
				String userName = loginLogic.fetchuserName(phoneNumber);
				custControl.setName(userName);
				Navigator.setPhoneNumber(phoneNumber);
				custControl.SetStage(custStage);
				Scene scene = new Scene(custPane);
				custStage.setTitle("SUPERRENT");
				custStage.setResizable(false);
				/*Rectangle2D bounds = Screen.getPrimary().getVisualBounds();
				custStage.setX(0);
				custStage.setY(0);
				custStage.setWidth(bounds.getWidth());
				custStage.setHeight(bounds.getHeight()); */	
				custStage.setScene(scene);
				custStage.show();
				PrevStage.close();
			}
			else if(role.equalsIgnoreCase(Constants.CLERK)){
				Stage clerkStage = new Stage();
				FXMLLoader myLoader = new  FXMLLoader(getClass().getResource(Navigator.CLERKFXML));
				Pane clerkPane = (Pane)myLoader.load();
				ClerkController clerkControl = (ClerkController) myLoader.getController();
				Navigator.setClerk(clerkControl);
				String phoneNumber = loginLogic.fetchPhoneNumber(login);
				String userName = loginLogic.fetchuserName(phoneNumber);
				clerkControl.setName(userName);
				Navigator.setPhoneNumber(phoneNumber);
				clerkControl.SetStage(clerkStage);
				Scene scene = new Scene(clerkPane);
				clerkStage.setTitle("SUPERRENT");
				clerkStage.setResizable(false);
				clerkStage.setScene(scene);
				clerkStage.show();
				PrevStage.close();
			}
			else if(role.equalsIgnoreCase(Constants.MANAGER)){
				Stage managerStage = new Stage();
				FXMLLoader myLoader = new  FXMLLoader(getClass().getResource(Navigator.MANAGERFXML));
				Pane managerPane = (Pane)myLoader.load();
				ManagerController managerControl = (ManagerController) myLoader.getController();
				Navigator.setManager(managerControl);
				String phoneNumber = loginLogic.fetchPhoneNumber(login);
				String userName = loginLogic.fetchuserName(phoneNumber);
				managerControl.setName(userName);
				Navigator.setPhoneNumber(phoneNumber);
				managerControl.SetStage(managerStage);
				Scene scene = new Scene(managerPane);
				managerStage.setTitle("SUPERRENT");
				managerStage.setResizable(false);
				managerStage.setScene(scene);
				managerStage.show();
				PrevStage.close();
			}
			else if(role.equalsIgnoreCase(Constants.SYSTEM_ADMIN)){
				Stage sysAdminStage = new Stage();
				FXMLLoader myLoader = new  FXMLLoader(getClass().getResource(Navigator.SYSADMINFXML));
				Pane sysAdminPane = (Pane)myLoader.load();
				SysAdminController sysAdminControl = (SysAdminController) myLoader.getController();
				Navigator.setSysAdmin(sysAdminControl);
				String phoneNumber = loginLogic.fetchPhoneNumber(login);
				String userName = loginLogic.fetchuserName(phoneNumber);
				sysAdminControl.setName(userName);
				Navigator.setPhoneNumber(phoneNumber);
				sysAdminControl.SetStage(sysAdminStage);
				Scene scene = new Scene(sysAdminPane);
				sysAdminStage.setTitle("SUPERRENT");
				sysAdminStage.setResizable(false);
				sysAdminStage.setScene(scene);
				sysAdminStage.show();
				PrevStage.close();
			}
			else{
				message.setText(role);
			}
		}}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			message.setText("Connection failed..Try again!");
		} catch (Exception e) {
			e.printStackTrace();
			message.setText("Connection failed..Try again!");
		}
	}
    
	/**
	 * This method is invoked when user clicks ForgotUsername/Pwd
	 * Precondition: user starts the application and selects ForgotUserName/Pwd
	 * Postcondition: ForgotUserNamePwd frame is loaded
	 * @param event
	 * @throws IOException
	 */
	@FXML
	public void forgotUserNamePwd(ActionEvent event) throws IOException{
		Stage detailsStage = new Stage();
		FXMLLoader myLoader = new  FXMLLoader(getClass().getResource("/userManagementUI/forgotUserNamePwd.fxml"));
		Pane adminDetailsPane = (Pane)myLoader.load();
		Scene scene = new Scene(adminDetailsPane);
		detailsStage.setScene(scene);
		detailsStage.setTitle("SUPERRENT");
		detailsStage.show();
	}
    
	/**
	 * This method is executed when user clicks newCustomer button
	 * Precondition: user starts application and clicks newCustomer
	 * Postcondition: createLogin form is loaded
	 * @param event
	 * @throws IOException
	 */
	@FXML
	public void createCustomer(ActionEvent event) throws IOException{
		Stage createUserStage = new Stage();
		FXMLLoader myLoader = new  FXMLLoader(getClass().getResource("/userManagementUI/createLogin.fxml"));
		Pane createCustomer = (Pane)myLoader.load();
		CreateUserController createUserControl = (CreateUserController) myLoader.getController();
		Scene scene = new Scene(createCustomer);
		createUserControl.SetStage(createUserStage);
		createUserStage.setTitle("SUPERRENT");
		createUserStage.setResizable(false);
		createUserStage.setScene(scene);
		createUserStage.show();
		PrevStage.close();
	}
	
	public void SetStage(Stage Stage)
	{
		this.PrevStage=Stage;
	}

	
	@Override
	public void initialize(URL location, ResourceBundle resources) 
	{
		Validator.addTextLimiter(loginId, 10);
		Validator.addTextLimiter(pwd, 15);
	}

}
